package com.ruoyi.project.wx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.bootdo.UuidUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.wx.domain.SalesCouponDO;
import com.ruoyi.project.wx.service.SalesCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 全局优惠券Controller
 *
 * @author long
 * @date 2021-04-15
 */
@RestController
@RequestMapping("/wx/SalesCoupon")
public class SalesCouponController extends BaseController
{
	@Autowired
	private SalesCouponService salesCouponService;

	/**
	 * 查询全局优惠券列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:SalesCoupon:list')")
	@GetMapping("/list")
	public TableDataInfo list(SalesCouponDO salesCoupon)
	{
		startPage();
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(salesCoupon);
		stringObjectMap.put("delFlag","0");
		stringObjectMap.put("sort","create_at");
		stringObjectMap.put("order","desc");
		List<SalesCouponDO> list = salesCouponService.list(stringObjectMap);
		return getDataTable(list);
	}

	/**
	 * 导出全局优惠券列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:SalesCoupon:export')")
	@Log(title = "全局优惠券", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(SalesCouponDO salesCoupon)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(salesCoupon);
		List<SalesCouponDO> list = salesCouponService.list(stringObjectMap);
		ExcelUtil<SalesCouponDO> util = new ExcelUtil<SalesCouponDO>(SalesCouponDO.class);
		return util.exportExcel(list, "SalesCoupon");
	}

	/**
	 * 获取全局优惠券详细信息
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:SalesCoupon:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{
		return AjaxResult.success(salesCouponService.get(id));
	}

	/**
	 * 新增全局优惠券
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:SalesCoupon:add')")
	@Log(title = "全局优惠券", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody SalesCouponDO salesCoupon)
	{
		salesCoupon.setId(UuidUtil.get32UUID());
		salesCoupon.setCreateAt(new Date());
		salesCoupon.setUpdateAt(new Date());
		salesCoupon.setHasScore(1);
		salesCoupon.setScore(new BigDecimal(0));
		salesCoupon.setType(1);
		salesCoupon.setSendNum(0);
		salesCoupon.setDelFlag(0);
		salesCoupon.setLimitNumber(1);
		return toAjax(salesCouponService.save(salesCoupon));
	}

	/**
	 * 修改全局优惠券
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:SalesCoupon:edit')")
	@Log(title = "全局优惠券", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody SalesCouponDO salesCoupon)
	{
		salesCoupon.setUpdateAt(new Date());
		return toAjax(salesCouponService.update(salesCoupon));
	}

	/**
	 * 删除全局优惠券
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('wx:SalesCoupon:remove')")
	@Log(title = "全局优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		if(ids.length>0){
			for (String string : ids) {
				SalesCouponDO salesCoupon = salesCouponService.get(string);
				salesCoupon.setDelFlag(1);
				salesCoupon.setUpdateAt(new Date());
				salesCouponService.update(salesCoupon);
			}
		}
		return toAjax(ids.length);
		//return toAjax(salesCouponService.deleteSalesCouponByIds(ids));
	}
}
